require 'View/UIBase/UIBase'

UISource = UIBase.New(PanelResNames.UISource)

function UISource.Init(func, id, num)
    UISource.itemId = id;
    UISource.needNum = num;
    UISource.goFunc = func;
    UIBase.Init(UISource);
end

-- 创建回调
function UISource:OnCreate()
    self.fristItem = self:GetChild("item");
    self.itemName = self:GetChild("itemName");
    self.itemNum = self:GetChild("num");
    self.itemNumRequire = self:GetChild("numRequire");
    self.text1 = self:GetChild("text1");
    self.title = self:GetChild("title");
    self.itemList = self:SetDynamicList(self:GetChild("list"),self.updateItem, nil, true)
    self.nothingController = self:GetController("nothing");
    self.stateController = self:GetController("state");
    self:addEvent();
end

-- 显示
function UISource:OnEnable()
    if self.itemId == nil then
        self:OnBack();
    else
        self:Refresh();
        if #self.goDataList > 0 then
            self.itemList:ScrollToView(0);
        end
    end
end

function UISource:addEvent()
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
    self:RegisterEvent(Config.EventType.Fresh_Currency,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_Bag,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_BagUseItemBack, logicMgr.ItemManager.showGetItem);
end

function UISource:removeEvent()
end

function UISource:Refresh()
    self:UpdateData();
end

function UISource:UpdateData()
    self.itemTableData = G.dataTable["t_item"][tostring(self.itemId)];
    if self.itemTableData == nil then
        self:OnBack();
        return ;
    end
    logicMgr.CostManager.RefreshItem(self.fristItem, {self.itemId});
    self.itemName.text = logicMgr.ItemManager.GetItemName(self.itemId);
    self.sumNum = logicMgr.ItemManager.getBagNumByTableID(self.itemId);
    self.itemNum.text = self.sumNum;
    if self.needNum ~= nil then
        self.stateController.selectedIndex = 1;
        self.itemNumRequire.text = self.needNum;
    else
        self.stateController.selectedIndex = 0;
    end
    self.goTableList = {};
    if self.itemTableData.f_source ~= "" then
        self.goTableList = decodeJsonStrToTabel(self.itemTableData.f_source);
        table.sort(self.goTableList, function (a, b) return a[2] > b[2] end);
    end
    self.goDataList = {};
    for k, v in pairs(self.goTableList) do
        self.sourceTableData = G.dataTable["t_source"][tostring(v[1])];
        if self.sourceTableData ~= nil then
            local data = {};
            data.id = v[1];
            data.recommend = v[2];
            data.type = self.sourceTableData.f_type;
            data.state = 0
            if data.type == 1 and logicMgr.ItemManager.getBagNumByTableID(self.sourceTableData.f_key) > 0 then
                data.state = 1
            end
            if data.type == 2 and logicMgr.FuncOpenManager.isOpen(self.sourceTableData.f_key) then
                data.state = 1
            end
            if data.state >= 1 then
                table.insert(self.goDataList, data);
            end
        end
    end
    table.sort(self.goDataList, function (a, b)
        if a.recommend > b.recommend then return true end;
        if a.recommend < b.recommend then return false end;
        if a.state > b.state then return true end;
        if a.state < b.state then return false end;
        return a.type < b.type;
    end);
    self.itemList.numItems = #self.goDataList;
    if #self.goDataList > 0 then
        self.nothingController.selectedIndex = 0;
    else
        self.nothingController.selectedIndex = 1;
    end
end

function UISource:updateItem(index, itemObj)
    local data = self.goDataList[index];
    local tableData = G.dataTable["t_source"][tostring(data.id)];
    local title = itemObj:GetChild("title");
    local item = itemObj:GetChild("item");
    local btn_Text = itemObj:GetChild("btn_Text");
    local btn_Use = itemObj:GetChild("btn_Use");
    local btn_Go = itemObj:GetChild("btn_Go");
    local typeController = itemObj:GetController("type");
    local isFuncController = itemObj:GetController("ifFunc");
    local isRecommendController = itemObj:GetController("ifRecommend");
    local isItemController = itemObj:GetController("ifItem");
    if data.recommend > 0 then
        isRecommendController.selectedIndex = 1;
    else
        isRecommendController.selectedIndex = 0;
    end
    if tableData.f_type == 2 then
        typeController.selectedIndex = 1;
        title.text = getLanguage(tableData.f_FunctionName);
        if data.state > 0 then
            isFuncController.selectedIndex = 0;
            btn_Go.onClick:Clear();
            btn_Go.onClick:Add(function() self:onGo(tableData.f_key); end);
        else
            isFuncController.selectedIndex = 1;
            btn_Text.onClick:Clear();
            btn_Text.onClick:Add(function() logicMgr.FuncOpenManager.isOpen(tableData.f_key, true,btn_Text); end);
        end
    else
        typeController.selectedIndex = 0;
        title.text = logicMgr.ItemManager.GetItemName(tableData.f_key);
        logicMgr.CostManager.RefreshItem(item, {tableData.f_key, logicMgr.ItemManager.getBagNumByTableID(tableData.f_key)});
        if data.state > 0 then
            isItemController.selectedIndex = 0;
            btn_Use.onClick:Clear();
            btn_Use.onClick:Add(function() self:onUseItem(tableData.f_key); end);
        else
            isItemController.selectedIndex = 1;
        end
    end
end

function UISource:onUseItem(key)
    local data = logicMgr.ItemManager.getItemConstDatabyModelId(key, true);
    data.needNum = self.needNum;
    data.itemId = logicMgr.ItemManager.getItemIdByModelId(key);
    showUI(PanelNames.UIBagItemInfo, data, 1);
end

function UISource:onGo(key)
    if UISource.goFunc then
        UISource.goFunc();
    end
    logicMgr.FuncOpenManager.jump(key, true);
    self:OnClose();
end

function UISource:showItem()
    local tabItems = logicMgr.ItemManager.useItemReturn();
    if table.getn(tabItems) > 0 then
        showUI(PanelNames.UICommonItemGet,nil,nil,3,tabItems)
    end
end

-- 关闭
function UISource:OnClose()
    self:OnBack();
    self:RemoveAllTimeCall();
    logicMgr.ActiveManager.showPopUpTips();
end

-- 隐藏
function UISource:OnDisable()
end

-- 卸载
function UISource:OnDestroy()
    self:removeEvent();
end

return UISource